//
//  CarouselView.m
//  QYer
//
//  Created by lanou on 16/8/5.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "CarouselView.h"

@interface CarouselView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;


@end

@implementation CarouselView

- (instancetype)initWithFrame:(CGRect)frame imageUrls:(NSArray *)imageUrls {
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.pagingEnabled = YES;
        [self addSubview:self.scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 20, frame.size.width, 20)];
        [self addSubview:self.pageControl];
        
        //配置滚动视图
        [self setupScrollViewByImageUrls:imageUrls];
        //PageControl点击事件
        [self.pageControl addTarget:self action:@selector(pageControlClick) forControlEvents:(UIControlEventValueChanged)];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
    }
    return self;
}

//配置滚动视图
- (void)setupScrollViewByImageUrls:(NSArray *)imageUrls {
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (imageUrls.count+1), 0);
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    for (NSInteger i = 0; i < imageUrls.count + 2; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
        
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrls.lastObject] placeholderImage:[UIImage imageNamed:@"占位图"]];
        } else if (i == imageUrls.count + 1) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrls.firstObject] placeholderImage:[UIImage imageNamed:@"占位图"]];
        } else {
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrls[i-1]] placeholderImage:[UIImage imageNamed:@"占位图"]];
        }
        [self.scrollView addSubview:imageView];
    }
    self.pageControl.numberOfPages = imageUrls.count;
}


#pragma mark ----- UIScrollViewDelegate -----

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x <= 0) {
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width * self.pageControl.numberOfPages, 0);
    } else if (scrollView.contentOffset.x >= (self.pageControl.numberOfPages+1) * self.frame.size.width) {
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    //修改page下标
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.frame.size.width - 1;
}


//用户开始拖拽时暂停定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer setFireDate:[NSDate distantFuture]];
}

//用户结束拖拽时开启定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}

//定时器方法
- (void)timerAction {
    NSInteger index = self.scrollView.contentOffset.x / self.frame.size.width + 1;
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = self.frame.size.width * index;
    
    [self.scrollView setContentOffset:offset animated:YES];
}

//pageControl点击方法
- (void)pageControlClick {
    //关闭定时器
    [self.timer setFireDate:[NSDate distantFuture]];
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake((self.pageControl.currentPage + 1) * self.frame.size.width, 0);
        
    } completion:^(BOOL finished) {
        //继续定时器
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
    }];
}

//图片点击方法
- (void)imageClickFunction {
    //计算当前显示的图片
    NSInteger index = self.scrollView.contentOffset.x / self.frame.size.width;
    //先判断block是否实现
    if (self.imageClickBlock) {
        
        if (self.scrollView.contentSize.width == self.frame.size.width) {
            self.imageClickBlock(index);
        } else {
            self.imageClickBlock(index - 1);
        }
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
